package com.viish.apps.tripletriad.views;

import com.viish.apps.tripletriad.carte.Card;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.Paint.Align;
import android.util.AttributeSet;
import android.widget.ImageView;

public class MinimalistCardView extends ImageView
{
	private Bitmap picture1, picture2 = null;
	private Paint paint;
	private Card card;
	private int posx, posy, taillePinceau;
	private boolean isChecked = false;
	
	private final float shadowRadius = 1.0f;
    private final int shadowOffset = 1;
    private final int shadowColor = 0xffffffff;
    
    private boolean showNumber;
    
	public MinimalistCardView(Context context, Bitmap picture, Card c, boolean showNumber)
	{
		super(context);
		this.card = c;
		this.showNumber = showNumber;
		
		initPaint();
		setImage(picture);
	}
	
	public MinimalistCardView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		showNumber = true;
		
		initPaint();
	}
	
	public MinimalistCardView clone(boolean showNumber)
	{
		return new MinimalistCardView(this.getContext(), this.picture1, this.card, showNumber);
	}
	
	public String getCardName()
	{
		return this.card.getName();
	}
	
	public int getCardLevel()
	{
		return this.card.getLevel();
	}

	public int getCardEdition() 
	{
		return this.card.getEdition();
	}
	
	private void initPaint()
	{
		this.paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "font.ttf");
		this.paint.setTypeface(typeface); 
		this.paint.setColor(0xff000000);
		this.paint.setTextAlign(Align.LEFT);
		this.paint.setTextSize(15);
		this.taillePinceau = 15;
	}
	
	public void setImage(Bitmap picture)
	{
		this.picture1 = picture;
		setImageBitmap(picture);
	}

	public void setChecked(boolean checked) 
	{
		isChecked = checked;
		invalidate();
	}
	
	public boolean isChecked()
	{
		return this.isChecked;
	}
	
	public void swapChecked() 
	{
		isChecked = !isChecked;
		invalidate();
	}
	
	public void setCard(Card c)
	{
		this.card = c;
		invalidate();
	}
	
	@Override
	protected void onDraw(Canvas canvas)
	{
		String top, left, bot, right, number;
		if (this.card.getTopValue() == 10) top = "A";
		else top = this.card.getTopValue() + "";
		if (this.card.getLeftValue() == 10) left = "A";
		else left = this.card.getLeftValue() + "";
		if (this.card.getBottomValue() == 10) bot = "A";
		else bot = this.card.getBottomValue() + "";
		if (this.card.getRightValue() == 10) right = "A";
		else right = this.card.getRightValue() + "";
		if (showNumber) number = "x" + this.card.getNumber();
		else number = "";
		
		if (isChecked && picture2 == null)
		{
			try
			{
				picture2 = BitmapFactory.decodeStream(getResources().getAssets().open(card.getFullName() + ".2.png"));
				canvas.drawBitmap(this.picture2, 0, 0, null);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		else if (isChecked) canvas.drawBitmap(this.picture2, 0, 0, null);
		else canvas.drawBitmap(this.picture1, 0, 0, null); 
			
		this.paint.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, shadowColor);
		canvas.drawText(top, posx + (3 * taillePinceau / 2 + 2) / 2, posy + taillePinceau, this.paint);
		this.paint.setShadowLayer(shadowRadius, shadowOffset, -shadowOffset, shadowColor);
		canvas.drawText(top, posx + (3 * taillePinceau / 2 + 2) / 2, posy + taillePinceau, this.paint);
		this.paint.setShadowLayer(shadowRadius, -shadowOffset, shadowOffset, shadowColor);
		canvas.drawText(top, posx + (3 * taillePinceau / 2 + 2) / 2, posy + taillePinceau, this.paint);
		this.paint.setShadowLayer(shadowRadius, -shadowOffset, -shadowOffset, shadowColor);
		canvas.drawText(top, posx + (3 * taillePinceau / 2 + 2) / 2, posy + taillePinceau, this.paint);
		
		this.paint.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, shadowColor);
		canvas.drawText(left, posx + 3, posy + 3 * taillePinceau / 2, this.paint);
		this.paint.setShadowLayer(shadowRadius, shadowOffset, -shadowOffset, shadowColor);
		canvas.drawText(left, posx + 3, posy + 3 * taillePinceau / 2, this.paint);
		this.paint.setShadowLayer(shadowRadius, -shadowOffset, shadowOffset, shadowColor);
		canvas.drawText(left, posx + 3, posy + 3 * taillePinceau / 2, this.paint);
		this.paint.setShadowLayer(shadowRadius, -shadowOffset, -shadowOffset, shadowColor);
		canvas.drawText(left, posx + 3, posy + 3 * taillePinceau / 2, this.paint);
		
		this.paint.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, shadowColor);
		canvas.drawText(bot, posx + (3 * taillePinceau / 2 + 2) / 2, posy + 2 * taillePinceau, this.paint);
		this.paint.setShadowLayer(shadowRadius, shadowOffset, -shadowOffset, shadowColor);
		canvas.drawText(bot, posx + (3 * taillePinceau / 2 + 2) / 2, posy + 2 * taillePinceau, this.paint);
		this.paint.setShadowLayer(shadowRadius, -shadowOffset, shadowOffset, shadowColor);
		canvas.drawText(bot, posx + (3 * taillePinceau / 2 + 2) / 2, posy + 2 * taillePinceau, this.paint);
		this.paint.setShadowLayer(shadowRadius, -shadowOffset, -shadowOffset, shadowColor);
		canvas.drawText(bot, posx + (3 * taillePinceau / 2 + 2) / 2, posy + 2 * taillePinceau, this.paint);
		
		this.paint.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, shadowColor);
		canvas.drawText(right, posx + (3 * taillePinceau / 2) - 2, posy + 3 * taillePinceau / 2, this.paint);
		this.paint.setShadowLayer(shadowRadius, shadowOffset, -shadowOffset, shadowColor);
		canvas.drawText(right, posx + (3 * taillePinceau / 2) - 2, posy + 3 * taillePinceau / 2, this.paint);
		this.paint.setShadowLayer(shadowRadius, -shadowOffset, shadowOffset, shadowColor);
		canvas.drawText(right, posx + (3 * taillePinceau / 2) - 2, posy + 3 * taillePinceau / 2, this.paint);
		this.paint.setShadowLayer(shadowRadius, -shadowOffset, -shadowOffset, shadowColor);
		canvas.drawText(right, posx + (3 * taillePinceau / 2) - 2, posy + 3 * taillePinceau / 2, this.paint);
		
		this.paint.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, shadowColor);
		canvas.drawText(number, picture1.getWidth() - taillePinceau, picture1.getHeight() - (taillePinceau / 2), this.paint);
		this.paint.setShadowLayer(shadowRadius, shadowOffset, -shadowOffset, shadowColor);
		canvas.drawText(number, picture1.getWidth() - taillePinceau, picture1.getHeight() - (taillePinceau / 2), this.paint);
		this.paint.setShadowLayer(shadowRadius, -shadowOffset, shadowOffset, shadowColor);
		canvas.drawText(number, picture1.getWidth() - taillePinceau, picture1.getHeight() - (taillePinceau / 2), this.paint);
		this.paint.setShadowLayer(shadowRadius, -shadowOffset, -shadowOffset, shadowColor);
		canvas.drawText(number, picture1.getWidth() - taillePinceau, picture1.getHeight() - (taillePinceau / 2), this.paint);
	}
}
